﻿using DFGK.IService.BBS;
using DFGK_WMS.Common;
using DFGK_WMS.Model.Models.BBS;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace DFGK_WMS.WebAPI.Controllers.Platform
{
    /// <summary>
    /// 资讯信息
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class InfomationController : BaseController
    {
        private readonly IInfomationService _infomationService;
        public InfomationController(IWebHostEnvironment environment, IInfomationService infomationService) : base(environment)
        {
            _infomationService = infomationService;
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="title"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetPageData(string? title, int pageIndex = 1, int pageSize = 50)
        {

            int total = 0;
            var page = _infomationService.GetInfomationPageData(title, out total, pageIndex, pageSize);
            return Ok(BaseResult.Result(page, "查询成功！", "200", total));
        }

        /// <summary>
        /// 删除资讯信息
        /// </summary>
        /// <param name="infomationId">资讯id</param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DelInfomation(long infomationId)
        {
            var result = _infomationService.DelInfomation(infomationId);
            if (result)
            {
                return Ok(BaseResult.Result(true, "删除成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "删除失败！", "500"));
        }

        /// <summary>
        /// 新增/修改公告类别
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult SubmitInfomation(Infomation model)
        {
            if (model.Id > 0)
            {
                model.ModifyUserId = base.BaseGetNowUserId();
                model.ModifyUserName = base.BaseGetNowUserName();
            }
            else
            {
                model.CreateUserId = base.BaseGetNowUserId();
                model.CreateUserName = base.BaseGetNowUserName();
            }
            bool result = _infomationService.SubmitInfomationData(model, out string message);
            if (result)
            {
                return Ok(BaseResult.Result(true, "添加成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "添加失败！" + message, "500"));
        }

        /// <summary>
        /// Id查询
        /// </summary>
        /// <param name="areaId"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetInfomation(long infomationId)
        {
            var cobj =_infomationService.GetInfomation(infomationId);
            if (cobj != null)
            {
                return Ok(BaseResult.Result(cobj, "查询成功！", "200", 0));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }
    }
}
